package com.sunsys.QuestionAnswer;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



//@WebServlet("/Answer")
public class QuestionAndAnswerRetrieve extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		response.setContentType("text/html");
		try {
			DBConnection connection = new DBConnection();
			Connection con =connection.makeDBConnection();
			Statement st = con.createStatement();
			int qid =(Integer.parseInt(request.getParameter("id")));
			HttpSession session = request.getSession(true);
			int userId = (Integer)session.getAttribute("userId");
			if(userId!=0) {
				ResultSet rs = st.executeQuery("select qname from questions where qid="+qid);
				if(rs.next()) {
					String question = rs.getString(1);
					request.setAttribute("question", question);
				}
				request.setAttribute("qid", qid);
				rs = st.executeQuery("select answer from answer where qid="+qid);
				List<String> answersList = new ArrayList<String>();
				while(rs.next()) {
					answersList.add(rs.getString(1));
				}
				request.setAttribute("answersList", answersList);
				RequestDispatcher rd = request.getRequestDispatcher("QuestionAndAnswerDisplayt.jsp");
				rd.forward(request, response);
			}
			else {
				out.println("With out Login You cannot Answer the questions");
			}
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
		
}
